<?php
declare(strict_types=1);

namespace Modules\Promanagement\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Promanagement\Models\Processdata;
use Illuminate\Http\Request;
use Modules\Promanagement\Http\Repositories\ProcessflowRequest;
use extend\services\warehouse\{warehouseService};//引入自定义类


class ProcessdataController extends Controller
{

   
    public function __construct(
        protected readonly Processdata $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(): mixed
    {
        $data = $this->model->getList();
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
       
       $processtype = [];
        foreach($this->model->processtype() as $k=>$v){
            $processtype[$v['value']] = $v['label'];
        }
    
       foreach($data as $k=>$v){
            $data[$k]['toallow'] = $v['toallow']==0?'否':'是';
            if(!empty($v['processtype'])){
                $data[$k]['processtype'] = isset($processtype[$v['processtype']])?$processtype[$v['processtype']]:'';
            }
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
       }
        return $data;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
       
        $data = $request->all();
        $res = $this->model->where('workingcode',$data['workingcode'])->first();
        if(!empty($res)){
           throw new FailedException('工序代号已存在请重新录入');
        }else{
           return $this->model->storeBy($request->all());
        }
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        return $this->model->firstBy($id);
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        
        // $data = $request->all();
        // $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
        // return $this->model->updateBy($id, $data);
        
        $data = $request->all();
        $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
        return ProcessflowRequest::createUpdate($id,$data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }

    /**
     * 工序类型接口
     * @return mixed
     */
    public function listtype()
    {
        return  $this->model->processtype();
    }


}
